package com.woniuxy.yoga.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.yoga.commons.core.model.PageInfo;
import com.woniuxy.yoga.order.dao.model.OrderLog;
import com.woniuxy.yoga.order.dao.mapper.OrderLogMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.yoga.order.model.param.ListOrderLogParam;
import com.woniuxy.yoga.order.model.param.SelectOrderLogParam;
import com.woniuxy.yoga.order.service.OrderLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单日志表 服务实现类
 * </p>
 *
 * @author 韦用文,刘存益,郎腾霄
 * @since 2023年12月05日
 */
@Service
public class OrderLogServiceImpl extends ServiceImpl<OrderLogMapper, OrderLog> implements OrderLogService {

    @Autowired
    private OrderLogMapper orderLogMapper;

    /**
     * 分页查看订单日志
     * @param param 分页查看订单日志参数
     * @return PageInfo
     */
    @Override
    public PageInfo listOrderLog(ListOrderLogParam param) {
        if (ObjectUtil.isEmpty(param.getPageNum()))
            param.setPageNum(1);
        if (ObjectUtil.isEmpty(param.getPageSize()))
            param.setPageSize(10);
        //创建查询对象
        QueryWrapper<OrderLog> wrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(param.getOrderNum()))
            wrapper.like("order_num",param.getOrderNum());
        if (ObjectUtil.isNotEmpty(param.getLogUser()))
            wrapper.like("log_user",param.getLogUser());
        wrapper.orderByDesc("log_time");
        //创建分页对象查询
        Page<OrderLog> page = new Page<>(param.getPageNum(), param.getPageSize());
        orderLogMapper.selectPage(page,wrapper);
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        return pageInfo;
    }

    /**
     * 商家分页查看订单日志
     * @param param 分页查看订单日志参数
     * @return PageInfo
     */
    @Override
    public PageInfo selectOrderLog(SelectOrderLogParam param) {
        if (ObjectUtil.isEmpty(param.getPageNum()))
            param.setPageNum(1);
        if (ObjectUtil.isEmpty(param.getPageSize()))
            param.setPageSize(10);
        //创建查询对象
        QueryWrapper<OrderLog> wrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(param.getOrderNum()))
            wrapper.like("order_num",param.getOrderNum());
        if (ObjectUtil.isNotEmpty(param.getLogUser()))
            wrapper.like("log_user",param.getLogUser());
        wrapper.eq("store_id",param.getStoreId());
        wrapper.orderByDesc("log_time");
        //创建分页对象查询
        Page<OrderLog> page = new Page<>(param.getPageNum(), param.getPageSize());
        orderLogMapper.selectPage(page,wrapper);
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        return pageInfo;
    }
}
